<?php

namespace App\Http\Controllers\background;
use App\Http\Controllers\Controller;
use Illuminate\Routing\Controller as BaseController;

class CommonController extends BaseController
{
    // 转数组
    public function totoArray($info){
        return json_decode(json_encode($info),true);
    }
    // 返回示例
    public function fails($message,$code=1,$info=[]){
        $arr = [
            'code'=>$code,
            'message'=>$message,
            'data'=>$info
        ];
        echo json_encode($arr,320 );
        $arr = null;
        exit;
    }

    public function successly($info=[],$code=2,$message='成功'){
        $arr = [
            'code'=>$code,
            'message'=>$message,
            'data'=>$info
        ];
        echo json_encode($arr,320 );
        $arr = null;
        exit;
        //echo "{'code':".$code.",'message':".$message.",'data':".[$info]."}";
    }

    // 数字转大写汉字
    function toChineseNumber($money){
        $money = round($money,2);
        $cnynums = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
        $cnyunits = array("圆","角","分");
        $cnygrees = array("拾","佰","仟","万","拾","佰","仟","亿");
        list($int,$dec) = explode(".",$money,2);
        $dec = array_filter(array($dec[1],$dec[0]));
        $ret = array_merge($dec,array(implode("",cnyMapUnit(str_split($int),$cnygrees)),""));
        $ret = implode("",array_reverse(cnyMapUnit($ret,$cnyunits)));
        return str_replace(array_keys($cnynums),$cnynums,$ret);
    }

    /**
    使用curl方式实现get或post请求
    @param $url 请求的url地址
    @param $data 发送的post数据 如果为空则为get方式请求
    return 请求后获取到的数据
     */
    function curlRequest($url,$data = ''){
        $ch = curl_init();
        $params[CURLOPT_URL] = $url;    //请求url地址
        $params[CURLOPT_HEADER] = false; //是否返回响应头信息
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向
        $params[CURLOPT_TIMEOUT] = 30; //超时时间
        if(!empty($data)){
            $params[CURLOPT_POST] = true;
            $params[CURLOPT_POSTFIELDS] = $data;
        }
        $params[CURLOPT_SSL_VERIFYPEER] = false;//请求https时设置,还有其他解决方案
        $params[CURLOPT_SSL_VERIFYHOST] = false;//请求https时,其他方案查看其他博文
        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        curl_close($ch); //关闭连接
        return $content;
    }

    //Xml转数组
    function XmlToArr($xml)
    {
        if($xml == '') return '';
        libxml_disable_entity_loader(true);
        $arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $arr;
    }

    // 硕软接口调用方法
    function srcurl($url, $data)
    {
        $url = trim($url);
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip, deflate'));
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $curl_result = curl_exec($ch);
        $curl_result = mb_convert_encoding($curl_result, 'utf-8', 'GBK,UTF-8,ASCII');
        $result_json = json_decode($curl_result, true);
        return $result_json;
    }

    /** 获取毫秒级别
     * @return float
     */
    function getMsecTime()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }

    function curlcheng_post($url, $data)

    {
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_HEADER, 0);//不抓取头部信息。只返回数据

        curl_setopt($curl, CURLOPT_TIMEOUT,10);//超时设置

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//1表示不返回bool值

        curl_setopt($curl, CURLOPT_POST, 1);

        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));//重点

        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));

        $response = curl_exec($curl);

        if (curl_errno($curl)) {
            return curl_error($curl);

        }

        curl_close($curl);

        return $response;
//header('Location:'.$response['data']['url']);

    }

    //sign生成
    function FL_sign($data){
        //签名步骤一：把字典json序列化
        $json = json_encode( $data, 320 );
        //签名步骤二：转化为数组
        $jsonArr = mb_str_split( $json );
        //签名步骤三：排序
        sort( $jsonArr );
        //签名步骤四：转化为字符串
        $string = implode( '', $jsonArr );
        //签名步骤五：在string后加入secret
        $string = $string . '9e69bb513bae465284b0253a9f4fe69e';
        //签名步骤六：MD5加密
        $result_ = strtolower( md5( $string ) );
        return $result_;
    }

    //福禄请求http
    function curl_post($url,$data){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//// 跳过证书检查
        curl_setopt($ch, CURLOPT_TIMEOUT, '60');
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data)
        ));
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            print curl_error($ch);
        }
        curl_close($ch);
        return $result;
    }

    //子轩post请求接口
    function zixuan_post($url, $data)
    {
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($curl, CURLOPT_HEADER, 0);//不抓取头部信息。只返回数据

        curl_setopt($curl, CURLOPT_TIMEOUT,1000);//超时设置

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//1表示不返回bool值

        curl_setopt($curl, CURLOPT_POST, 1);

        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));//重点

        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));

        $response = curl_exec($curl);

        if (curl_errno($curl)) {
            return curl_error($curl);

        }

        curl_close($curl);

        return $response;
    }

    function yunfacurl_https($url, $data = array(), $contentType = '', $header = array(), $timeout = 30, $debug = false) {
        $ch = curl_init();
        $data = $contentType == 'json' ? urldecode(json_encode($data)) : http_build_query($data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        if(!empty($data)){
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }


        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        $response = curl_exec($ch);

        if ($error = curl_error($ch)) {
            die($error);
        }

        // 打印错误信息
        if ($debug) {

            echo '=====info====='."\r\n";
            print_r(curl_getinfo($ch));

            echo '=====error====='."\r\n";
            print_r(curl_error($ch));

            echo '=====$response====='."\r\n";
            print_r($response);

        }
        curl_close($ch);
        return $response;
    }

    function baimiaorequest($url,$post,$headers){
        //初始化
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //设置请求头
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        //设置头文件的信息作为数据流输出
        //curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        return $data;
    }



    /**
     * 获取当前域名
     * @return string
     */
    function getHttpType()
    {
        $http = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
        return $http.$_SERVER['HTTP_HOST'];
    }
}
